package com.edu.Controller;

import com.edu.entity.User;
import com.edu.service.UserService;
import com.edu.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username=request.getParameter("username");
        String userpass=request.getParameter("userpass");
        if (username.equals("")||userpass.equals("")){
            request.setAttribute("meg","账号或者密码不能为空");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
        UserService us=new UserServiceImpl();
        User u=us.selectUserByPass(username,userpass);
        if (u==null) {
            boolean x=us.selectUser(username);
            if(x){
                request.setAttribute("meg","密码错误");
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }else{
                request.setAttribute("meg","没有该账号");
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }
        }else{
            System.out.println(u.getName());
            request.setAttribute("meg",u.getName());
        }
        request.getRequestDispatcher("BookServlet?meg="+u.getName()).forward(request, response);
    }


}
